package com.zhuzhe.upload;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhuzhe
 * @email zhuzhe_mail@163.com
 * @date 2024-06-17 13:55
 */
public class UploadImage {

    // 设置上传阿里云oss的信息  AliyunConfig

    // 本地保存文件路径
    public static final String LOCAL_SAVE_PATH = "F://test-image/";
    // 上传服务器文件路径
    public static final String SERVER_SAVE_PATH = "goods/temp/";

    public static final String STATIC_URL = "https://statics.xxxx.com/";

    static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    static OSSClient ossClient = new OSSClient(AliyunConfig.ossEndpoint, AliyunConfig.accessKey, AliyunConfig.accessKeySecret);

    public static void main(String[] args) {
        try {
            Image image = getImageFromClipboard();
            if (image != null) {
                BufferedImage image1 = toBufferedImage(image);
                String format = df.format(new Date()) + ".png";
                String fileName = LOCAL_SAVE_PATH + format;
                ImageIO.write(image1, "png", new File(fileName));
                new UploadImage().upload(fileName, SERVER_SAVE_PATH + format);
            }
        } catch (Exception e) {
            // do nothing
        }
    }

    public void upload(String filePath, String serverPath) {
        try {
            // 设置文件类型
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(this.getFileContentType(filePath));
            try {
                ossClient.putObject(AliyunConfig.ossBucket, serverPath, new FileInputStream(filePath), objectMetadata);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("上传失败");
            }
            // 将返回信息放入粘贴板中   ![xxx](https://statics.xxxx.com/xxx/xxx/xxxx.png)
            String markdown = "![" + "xxx" + "](" + STATIC_URL + serverPath + ")";
            StringSelection stringSelection = new StringSelection(markdown);
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, null);
        } catch (Exception e) {
        }
    }


    public static Image getImageFromClipboard() throws Exception {
        Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable cc = sysc.getContents(null);
        if (cc == null)
            return null;
        else if (cc.isDataFlavorSupported(DataFlavor.imageFlavor))
            return (Image) cc.getTransferData(DataFlavor.imageFlavor);
        return null;
    }

    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();

        // Return the buffered image
        return bimage;
    }

    public static String getFileContentType(String originalFilename) {


        if (originalFilename.toLowerCase().endsWith(".bmp")) {
            return "image/bmp";
        }
        if (originalFilename.toLowerCase().endsWith(".gif")) {
            return "image/gif";
        }
        if (originalFilename.toLowerCase().endsWith(".jpeg") || originalFilename.toLowerCase().endsWith(".jpg") || originalFilename.toLowerCase().endsWith(".png")) {
            return "image/jpg";
        }
        if (originalFilename.toLowerCase().endsWith(".html")) {
            return "text/html";
        }
        if (originalFilename.toLowerCase().endsWith(".txt")) {
            return "text/plain";
        }
        if (originalFilename.toLowerCase().endsWith(".vsd")) {
            return "application/vnd.visio";
        }
        if (originalFilename.toLowerCase().endsWith(".pptx") ||
                originalFilename.toLowerCase().endsWith(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (originalFilename.toLowerCase().endsWith(".docx") ||
                originalFilename.toLowerCase().endsWith(".doc")) {
            return "application/msword";
        }
        if (originalFilename.toLowerCase().endsWith(".xml")) {
            return "text/xml";
        }
        if (originalFilename.toLowerCase().endsWith(".pdf")) {
            return "application/pdf";
        }
        if (originalFilename.toLowerCase().endsWith(".zip")) {
            return "application/zip";
        }
        if (originalFilename.toLowerCase().endsWith(".mp3")) {
            return "audio/mp3";
        }
        if (originalFilename.toLowerCase().endsWith(".mp4")) {
            return "video/mp4";
        }
        if (originalFilename.toLowerCase().endsWith(".mov")) {
            return "video/quicktime";
        }
        return "image/jpg";
    }


}
